Return an error instead of throwing on invalid UTF-8 in deserialisation helpers#1249
Return an error instead of throwing on invalid UTF-8 in deserialisation helpers#1249palas wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens Cardano API deserialisation helpers by avoiding Text.decodeUtf8’s impure exception on invalid UTF-8, ensuring these helpers return normal decoding errors instead of crashing.
Changes:
- Switched Bech32 deserialisation paths in
deserialiseInput/deserialiseInputAnyOfto useText.decodeUtf8'and treat invalid UTF-8 as a format mismatch. - Updated
deserialiseAnyVerificationKeyBech32to avoid throwing on invalid UTF-8 and return a decoding error instead. - Added a Herald changelog fragment documenting the bugfix.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| cardano-api/src/Cardano/Api/Serialise/DeserialiseAnyOf.hs | Avoids impure exceptions by using decodeUtf8' and mapping invalid UTF-8 to Bech32 format mismatch. |
| cardano-api/src/Cardano/Api/Key/Internal/SomeAddressVerificationKey.hs | Prevents invalid UTF-8 from throwing during Bech32 verification key deserialisation. |
| .changes/20260708_140000_cardano-api_pablo.lamela_nonthrowing_utf8_decode.yml | Changelog fragment describing the bugfix behavior change. |
Jimbo4350
left a comment
There was a problem hiding this comment.
We shouldn't throw away the UnicodeException error. It needs to be rendered.
Instead of discarding the UnicodeException and fabricating StringToDecodeContainsInvalidChars [], deserialiseAnyVerificationKeyBech32 now returns Bech32InvalidUtf8 carrying the rendered decoding error.
Restructure the bech32 branches of deserialiseInput and deserialiseInputAnyOf as suggested in review.
| -- ^ Unexpected header | ||
| | -- | The input is not valid UTF-8, so it cannot be a Bech32-encoded | ||
| -- string. The field contains the rendered UTF-8 decoding error. | ||
| Bech32InvalidUtf8 !Text |
There was a problem hiding this comment.
Why the field is Text and not the exception itself? I'd suggest moving of rendering of the exception to Pretty instance instead of keeping it in two places.
Jimbo4350
left a comment
There was a problem hiding this comment.
2 comments to address.
| -- The input was not valid UTF-8, so it cannot be valid Bech32. | ||
| decodeText = | ||
| first (const DeserialiseInputErrorFormatMismatch) | ||
| . Text.decodeUtf8' |
There was a problem hiding this comment.
We are still throwing away the exception error here.
| deserialiseBech32 = either id fromBech32 $ decodeText inputBs | ||
| where | ||
| -- The input was not valid UTF-8, so it cannot be valid Bech32. | ||
| decodeText = |
There was a problem hiding this comment.
We are still throwing away the exception error here.
Context
deserialiseInput,deserialiseInputAnyOfanddeserialiseAnyVerificationKeyBech32(and therebydeserialiseAnyVerificationKey) usedText.decodeUtf8, which throws an impure exception when the input is not valid UTF-8. For example,deserialiseInputon the bytes"\xc3\x28"threw instead of returningLeft InputInvalidError.This PR switches them to
Text.decodeUtf8'and treats invalid UTF-8 as a normal decoding failure.How to trust this PR
DeserialiseInputErrorFormatMismatch/Bech32DecodingError) does not change behaviour for any valid input.Rightcase ofdecodeUtf8'.Checklist
.changes/